package net.sf.jOpinionPoll.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name="conteudo")
@NamedQueries({
        @NamedQuery(name = "Conteudo.findAll", query = "SELECT c FROM Conteudo c ORDER BY c.ordem"),
        @NamedQuery(name = "Conteudo.count", query = "SELECT COUNT(c) FROM Conteudo c")
         })
public class Conteudo implements Serializable {
	private static final long serialVersionUID = 3807016677807081904L;
	
	private int id;
	private String nome;
	private int ordem;
	private boolean bloqueado;
	private boolean habilitado;
	private String descricao;
	private Set<Papel> papeis = new HashSet<Papel>();
	
	@Id
	@Column(name = "id")
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name = "ordem", nullable = false)
	public int getOrdem() {
		return ordem;
	}
	
	public void setOrdem(int ordem) {
		this.ordem = ordem;
	}
	
	@Column(name = "nome", nullable = false)
	public String getNome() {
		return nome;
	}
	
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	@Column(name = "bloqueado", nullable = false)
	public boolean isBloqueado() {
		return bloqueado;
	}
	
	public void setBloqueado(boolean bloqueado) {
		this.bloqueado = bloqueado;
	}
	
	@Column(name = "habilitado", nullable = false)
	public boolean isHabilitado() {
		return habilitado;
	}

	public void setHabilitado(boolean habilitado) {
		this.habilitado = habilitado;
	}
	
	@Column(name = "descricao")
	public String getDescricao() {
		return descricao;
	}
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	
	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name="conteudo_papel",
		joinColumns=@JoinColumn(name="conteudo_id",
			referencedColumnName="id"),
		inverseJoinColumns=@JoinColumn(name="papel_nome",
			referencedColumnName="nome"))
    public Set<Papel> getPapeis() {
    	return papeis;
    }
    
    public void setPapeis(Set<Papel> p) {
    	papeis = p;
    }
    
    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        return getId();
    }
    
    @Override
	public boolean equals(Object o) {
    	if (o == null || !(o instanceof Conteudo))
            return false;

        if (getId() != 0)
	        return (getId() == ((Conteudo)o).getId());
        
        return this == o;
	}
    
	@Override
	public String toString() {
        return this.getClass().getName() + "[pk=" + getId() + "]";
	}
}
